/*
 * Created by JFormDesigner on Mon Jun 05 21:23:40 CST 2023
 */

package gui.admin;

import dao.ProductCategoryDao;
import dao.ProductDao;
import dao.UserDao;
import entity.Product;
import entity.ProductCategory;
import entity.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class AddProductFrame extends JFrame {

    public AddProductFrame() throws Exception {
        initComponents();
    }

    private void initComponents() throws Exception {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        productId1 = new JLabel();
        productName1 = new JLabel();
        label31 = new JLabel();
        expiryDate1 = new JLabel();
        storageLocation1 = new JLabel();
        purchasePrice1 = new JLabel();
        salePrice1 = new JLabel();
        stockQuantity1 = new JLabel();
        productId = new JTextField();
        productName = new JTextField();
        expiryDate = new JTextField();
        storageLocation = new JTextField();
        purchasePrice = new JTextField();
        salePrice = new JTextField();
        stockQuantity = new JTextField();
        addBtn = new JButton();
        categoryNameCbox = new JComboBox();
        cancelBtn = new JButton();

        ProductCategoryDao productCategoryDao = new ProductCategoryDao();

        List productCategoryList = productCategoryDao.getAllProductCategory();//获取所有类别
        for (int i = 0; i < productCategoryList.size(); i++) {
            ProductCategory productCategory = (ProductCategory) productCategoryList.get(i);
            //获取商品分类名，添加进下拉框
            categoryNameCbox.addItem(new ProductCategory(productCategory.getCategoryId(),productCategory.getCategoryName()){
                public String toString(){
                    String productCategoryName = productCategory.getCategoryName();
                    return productCategoryName;
                }
            });
        }

        //======== this ========
        setPreferredSize(new Dimension(500, 700));
        setTitle("\u6dfb\u52a0\u5546\u54c1\u4fe1\u606f");
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- productId1 ----
        productId1.setText("\u5546\u54c1\u7f16\u53f7");
        productId1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(productId1);
        productId1.setBounds(80, 45, 90, 40);

        //---- productName1 ----
        productName1.setText("\u5546\u54c1\u540d\u79f0");
        productName1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(productName1);
        productName1.setBounds(80, 95, 90, 40);

        //---- label31 ----
        label31.setText("\u5546\u54c1\u7c7b\u522b");
        label31.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label31);
        label31.setBounds(80, 150, 90, 40);

        //---- expiryDate1 ----
        expiryDate1.setText("\u4fdd\u8d28\u671f");
        expiryDate1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(expiryDate1);
        expiryDate1.setBounds(80, 200, 90, 40);

        //---- storageLocation1 ----
        storageLocation1.setText("\u5b58\u653e\u4f4d\u7f6e");
        storageLocation1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(storageLocation1);
        storageLocation1.setBounds(80, 255, 90, 40);

        //---- purchasePrice1 ----
        purchasePrice1.setText("\u8fdb\u4ef7");
        purchasePrice1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(purchasePrice1);
        purchasePrice1.setBounds(80, 305, 90, 40);

        //---- salePrice1 ----
        salePrice1.setText("\u552e\u4ef7");
        salePrice1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(salePrice1);
        salePrice1.setBounds(80, 360, 90, 40);

        //---- stockQuantity1 ----
        stockQuantity1.setText("库存数量");
        stockQuantity1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(stockQuantity1);
        stockQuantity1.setBounds(80, 415, 90, 40);
        contentPane.add(productId);
        productId.setBounds(190, 50, 220, 40);
        contentPane.add(productName);
        productName.setBounds(190, 100, 220, 40);
        contentPane.add(expiryDate);
        expiryDate.setBounds(190, 200, 220, 40);
        contentPane.add(storageLocation);
        storageLocation.setBounds(190, 255, 220, 40);
        contentPane.add(purchasePrice);
        purchasePrice.setBounds(190, 305, 220, 40);
        contentPane.add(salePrice);
        salePrice.setBounds(190, 360, 220, 40);
        contentPane.add(stockQuantity);
        stockQuantity.setBounds(190, 415, 220, 40);

        //---- AddBtn ----
        addBtn.setText("\u6dfb\u52a0");
        addBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(addBtn);
        addBtn.setBounds(235, 500, 90, 40);
        //给按钮添加点击事件
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String productIdTemp = productId.getText();//获取输入框的文本
                String productNameTemp = productName.getText();
                String expiryDateTemp = expiryDate.getText();
                String storageLocationTemp = storageLocation.getText();
                float purchasePriceTemp = Float.parseFloat(purchasePrice.getText());
                float salePriceTemp = Float.parseFloat(salePrice.getText());
                int stockQuantityTemp = Integer.parseInt(stockQuantity.getText());
                String productPictureTemp = productNameTemp + ".jpg";
                int index = categoryNameCbox.getSelectedIndex();//获取当前选中的商品类别的索引
                ProductCategory productCategory = (ProductCategory) productCategoryList.get(index);
                String categoryIdTemp = productCategory.getCategoryId();//商品类别编号
                String categoryNameTemp = productCategory.getCategoryName();//商品类别名称
                //构造一个商品对象
                Product product = new Product(productIdTemp, categoryIdTemp, categoryNameTemp, productNameTemp, expiryDateTemp,storageLocationTemp,purchasePriceTemp,salePriceTemp,stockQuantityTemp,productPictureTemp);
                ProductDao productDao = new ProductDao();

                boolean result = false;
                try {
                    result = productDao.addProduct(product);//调用添加方法
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                if (result) {
                    JOptionPane.showMessageDialog(null, "添加成功");
                } else {
                    JOptionPane.showMessageDialog(null, "添加失败");
                }
            }
        });

        contentPane.add(categoryNameCbox);
        categoryNameCbox.setBounds(190, 150, 220, 40);

        //---- CancelBtn ----
        cancelBtn.setText("\u8fd4\u56de");
        cancelBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(cancelBtn);
        cancelBtn.setBounds(360, 500, 90, 40);

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();//关闭当前窗口
                try {
                    AdminMainFrame adminMainFrame = new AdminMainFrame();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel productId1;
    private JLabel productName1;
    private JLabel label31;
    private JLabel expiryDate1;
    private JLabel storageLocation1;
    private JLabel purchasePrice1;
    private JLabel salePrice1;
    private JLabel stockQuantity1;
    private JTextField productId;
    private JTextField productName;
    private JTextField expiryDate;
    private JTextField storageLocation;
    private JTextField purchasePrice;
    private JTextField salePrice;
    private JTextField stockQuantity;
    private JButton addBtn;
    private JComboBox categoryNameCbox;
    private JButton cancelBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) throws Exception {
        AddProductFrame addProductFrame = new AddProductFrame();
    }
}
